<?php
namespace app\common\model;

use think\Model;
class Core extends Model
{
    /**
     * 验证字段值是否唯一
     * @Author   Laifuzi
     * @DateTime 2018-06-06
     * @param    string     $field_name  字段名
     * @param    string     $field_value 字段值
     * @param    integer    $id          当前记录主键
     * @return   boolean
     */
    public function isFieldValueUnique($field_name, $field_value, $id){
        $unique_field_names = $this->uniqueFileList();
        if(!in_array($field_name, $unique_field_names)){
            return false;
        }
        if(empty($field_value)){
            return false;
        }
        $count = $this->where('id', '<>', $id)->where($field_name, $field_value)->count();
        if($count > 0){
            return false;
        }
        return true;
    }

    /**
     * 删除单条记录
     * @Author   zhanghong(Laifuzi)
     * @DateTime 2018-06-12
     * @param    integer            $id [description]
     * @return   boolean                [description]
     */
    public function deleteItem($id){
        $can_res = $this->canDeleteItem($id);
        if(!$can_res['status']){
            return $can_res;
        }

        $res = ['status' => false, 'data' => [], 'msg' => '删除失败'];
        $id = intval($id);
        $item = $this->where('id', $id)->find();
        if(empty($item)){
            $res = [
                'status' => true,
                'msg' => '该记录已删除',
                'data' => ['id' => $id],
            ];
            return $res;
        }
        $item->delete();
        // 因为模型事件只在调用模型的方法生效，使用查询构造器操作是无效的
        // 所以我们需要添加一个afterDeleteItem方法
        $this->afterDeleteItem($item);
        $res = [
            'status' => true,
            'msg' => '删除成功',
            'data' => ['id' => $id],
        ];
        return $res;
    }

    /**
     * 验证是否可以删除当前记录
     * @Author   zhanghong(Laifuzi)
     * @DateTime 2018-06-12
     * @param    [type]             $id [description]
     * @return   [type]                 [description]
     */
    protected function canDeleteItem($id){
        return [
            'status' => true,
            'msg' => '可以删除当前记录',
        ];
    }

    /**
     * 删除记录后的执行方法
     * @Author   zhanghong(Laifuzi)
     * @DateTime 2018-06-12
     * @param    array              $item [description]
     * @return   boolean                  [description]
     */
    protected function afterDeleteItem($item){
        return true;
    }

    /**
     * 对记录进行重新排序
     * @Author   zhanghong(Laifuzi)
     * @DateTime 2018-06-11
     * @param    array              $items [description]
     * @return   array                     [description]
     */
    public function resortItems($items){
        $res = ['status' => false, 'msg' => "排序失败"];
        if(!is_array($items)){
            $res['msg'] = '排序对象必须是数组';
            return $res;
        }
        foreach ($items as $id => $sort_num) {
            $sort_num = intval($sort_num);
            if($sort_num < 0){
                $sort_num = 0;
            }else if($sort_num >= 9999){
                $sort_num = 9999;
            }
            $this->save(["sort" => $sort_num], ["id" => $id]);
        }
        $this->afterResort();
        return ['status' => true, 'msg' => "排序成功"];
    }

    /**
     * 排序后的操作
     * @Author   zhanghong(Laifuzi)
     * @DateTime 2018-06-11
     * @return   boolean                   [description]
     */
    protected function afterResort(){
        return true;
    }

    /**
     * 自动完成member字段赋值
     * @Author   zhanghong(Laifuzi)
     * @DateTime 2018-06-11
     */
    protected function setMemberIdAttr()
    {
        $member_id = login_member_id();
        return $member_id;
    }

    /**
     * 自动完成member字段赋值
     * @Author   zhanghong(Laifuzi)
     * @DateTime 2018-06-11
     */
    protected function setUserIdAttr()
    {
        $member_id = login_user_id();
        return $member_id;
    }

    /**
     * 验证手机号码格式是否正确
     * @Author   zhanghong(Laifuzi)
     * @DateTime 2018-06-25
     * @param    [type]             $mobile [description]
     * @return   array
     */
    protected function validMobile($mobile){
        $res = ['status' => false, 'msg' => '手机号码格式不正确'];
        if(empty($mobile)){
            $res['msg'] = '手机号码不能为空';
            return $res;
        }else if($mobile < 13000000000 || $mobile > 19999999999){
            $res['msg'] = '请填写有效的手机号码';
            return $res;
        }
        $res = ['status' => true, 'msg' => '手机号码格式正确'];
        return $res;
    }

    /**
     * 搜索排序方式
     * @Author   zhanghong(Laifuzi)
     * @DateTime 2018-06-07
     * @param    string             $param 排序方式
     * @return   string                    [description]
     */
    protected function queryOrderStr($order_type = '')
    {
        return 'id ASC';
    }

    /**
     * 唯一值字段列表
     * @Author   zhanghong(Laifuzi)
     * @DateTime 2018-06-07
     * @return   array             [description]
     */
    protected function uniqueFileList()
    {
        return [];
    }
}